﻿Public Class CI4100

#Region "Structures"

    Public Structure ResultStruct
        Dim TestCode As String
        Public DataValue() As String
        Public Unit As String
        Dim DateTestCompleted As String
        Public WhichModule As String
        Public BarcodeTest As String
    End Structure

    Public Structure Patient
        Dim PatientName() As String
        Dim Age As String
        Dim Sex As String
        Dim DateOfBirth As String
        Dim PhysicianID As String
        Dim Location As String
        Dim PID As String
        Dim SpecimenID() As String
        Dim ResultArray() As ResultStruct
        Dim Comment As String
    End Structure

#End Region

    Private Const ComponentDelimiter As Char = "^"
    Private Const RecordDelimiter As Char = vbCr
    Private Const FrameDelimiter As Char = vbCrLf
    Public Const FieldDelimiter As Char = "|"
    Private Const RepeatDelimiter As String = "\"
    Public Const ETB As Byte = &H17
    Public Const OrderRecordType As String = "O"
    Private Const PatientRecordType As String = "P"
    Private Const ResultRecordType As String = "R"

    Public Function FrameCheck(ByVal sFrameInput As String, ByRef FinalFrame As Boolean, ByRef sDataSegmentOutput As String) As Boolean
        Dim checksum1, checksum2 As Byte
        Dim sTemp As String
        checksum1 = Convert.ToByte(sFrameInput.Substring(sFrameInput.Length - 2), 16)
        sTemp = sFrameInput.Remove(sFrameInput.Length - 2)
        sTemp = sTemp.Substring(1)
        checksum2 = StringChecksum(sTemp)
        If checksum2 <> checksum1 Then
            Return False
        Else
            sDataSegmentOutput = sTemp.Remove(sTemp.Length - 1)     'Loc bo ETX hoac ETB
            sDataSegmentOutput = sDataSegmentOutput.Substring(1)            'Loc bo Frame No.
            If Asc(Convert.ToChar(sTemp.Substring(sTemp.Length - 1))) = ETB Then
                FinalFrame = False
            Else
                FinalFrame = True
            End If
            Return True
        End If
    End Function

    Public Function GetQuery(ByVal sDataSegment As String, ByRef sQueryOutput As String, ByRef CancelQuery As Boolean) As Boolean
        Dim sRecordTemp As String()
        Dim sTemp1, sTemp2 As String()
        Dim s As String
        Dim NotQuery As Boolean = True
        sRecordTemp = sDataSegment.Split(RecordDelimiter)
        For Each s In sRecordTemp
            sTemp1 = s.Split(FieldDelimiter)
            If sTemp1(0) = "Q" Then
                If sTemp1(2).Length > 0 Then
                    sTemp2 = sTemp1(2).Split(ComponentDelimiter)
                    sQueryOutput = sTemp2(1)
                    CancelQuery = False
                Else
                    CancelQuery = True
                End If
                If NotQuery Then
                    NotQuery = False
                End If
                Exit For
            End If
        Next
        Return (Not NotQuery)
    End Function

    Public Function GetResult(ByVal sDataSegment As String, ByRef sRecordOutput() As String) As Boolean
        Dim sTemp1 As String()
        Dim s As String
        Dim NotResult As Boolean = True
        sRecordOutput = sDataSegment.Split(RecordDelimiter)
        For Each s In sRecordOutput
            sTemp1 = s.Split(FieldDelimiter)
            If sTemp1(0) = ResultRecordType Then
                If NotResult Then
                    NotResult = False
                End If
                Exit For
            End If
        Next
        Return (Not NotResult)
    End Function

    Public Function GetTerminationStatus(ByVal sDataSegment As String) As Boolean
        Dim sRecordTemp As String()
        Dim NotEnd As Boolean = True
        Dim sTemp1 As String()
        sRecordTemp = sDataSegment.Split(RecordDelimiter)
        For Each s As String In sRecordTemp
            sTemp1 = s.Split(FieldDelimiter)
            If sTemp1(0) = "L" Then
                'If sTemp1(2).Length > 0 And sTemp1(2) = "F" Then
                NotEnd = False
                Exit For
                'End If
            End If
        Next
        Return (Not NotEnd)
    End Function

    Public Function CreateNegQueryRecord(ByVal SID As String)
        Dim stemp(12) As String
        For i As Byte = 0 To 12
            stemp(i) = String.Empty
        Next
        stemp(0) = "Q"
        stemp(1) = "1"
        stemp(2) = "^" & SID
        stemp(4) = "^^^ALL"
        stemp(12) = "X"
        Return [String].Join(FieldDelimiter, stemp)
    End Function

    Public Function CreatOrderRecord(ByVal SeqNo As Byte, ByVal SID As String, ByVal RackNoID As String, ByVal SamplePosID As String, ByVal TestID As String()) As String
        Dim sTemp(0 To 25) As String
        Dim sTemp2(0 To 2) As String
        Dim i As Byte
        Dim s As String = ""
        For i = 0 To 25
            sTemp(i) = ""
        Next i
        '  s = [String].Join(FieldDelimiter, sTemp)
        'For i = 0 To 2
        '    sTemp2(i) = ""
        'Next i
        'sTemp(2) = [String].Join(ComponentDelimiter, sTemp2)
        sTemp(0) = OrderRecordType
        If SeqNo > 0 Then
            sTemp(1) = SeqNo.ToString()
        Else
            MsgBox("Error In Sequence Number")
            Return [String].Empty
        End If
        sTemp2(0) = SID
        If Not [String].IsNullOrEmpty(RackNoID) Then
            sTemp2(1) = RackNoID
            If Not [String].IsNullOrEmpty(SamplePosID) Then
                sTemp2(2) = SamplePosID
                sTemp(2) = [String].Join(ComponentDelimiter, sTemp2)
            Else
                sTemp(2) = [String].Join(ComponentDelimiter, sTemp2, 0, 2)
            End If

        Else
            sTemp(2) = SID
        End If


        'For i = 0 To TestID.Length - 1
        '    TestID(i) = [String].Concat("^^^", TestID(i))
        'Next
        sTemp(4) = [String].Join(RepeatDelimiter, TestID)
        'sTemp(5) = "R" 'Routine
        sTemp(11) = "A" ' Add order for Patient
        sTemp(25) = "Q" ' Order and Response to query
        s = [String].Join(FieldDelimiter, sTemp)
        Return s
    End Function

    Public Function CreatePatientRecord(ByVal SeqNo As Byte, ByVal PID As String, ByVal Name As String) As String
        Dim sTemp(0 To 25) As String
        sTemp(0) = PatientRecordType
        If SeqNo > 0 Then
            sTemp(1) = SeqNo.ToString()
        Else
            MsgBox("Error In Sequence Number")
            Return [String].Empty
        End If
        If PID.Length > 0 Then
            sTemp(2) = PID
        End If

        sTemp(5) = String.Concat(Name.Trim, "^^")

        Return [String].Join(FieldDelimiter, sTemp)
    End Function

    Public Function CreateTerminationRecord(ByVal SeqNo As Byte, ByVal TerminationCode As String) As String
        Dim sTemp(0 To 2) As String
        sTemp(0) = TerminationRecordType
        If SeqNo > 0 Then
            sTemp(1) = SeqNo.ToString()
        Else
            MsgBox("Error In Sequence Number")
            Return [String].Empty
        End If
        sTemp(2) = TerminationCode
        Return [String].Join(FieldDelimiter, sTemp)
    End Function

    Private Function StringChecksum(ByVal sInput As String) As Byte
        Dim bSTemp() As Char
        Dim bChecksum As Int16 = 0
        bSTemp = sInput.ToCharArray()
        For Each CharinArr As Char In bSTemp
            bChecksum += Asc(CharinArr)
            bChecksum = bChecksum Mod &H100
        Next
        Return bChecksum
    End Function

    Public Overloads Function CreateSinglePatientFrame(ByVal HeaderRecord As String, ByVal PatientRecord As String, ByVal OrderRecord As String, ByVal TerminationRecord As String) As String()
        'Dim sTemp, sChecksum As String
        'Dim Checksum As Byte = 0
        'Dim arrSTEMP(3) As String
        'Dim arrOrder As New ArrayList
        'sTemp = [String].Concat(Chr(2), "1", HeaderRecord, vbCr, Chr(3))
        'Checksum = StringChecksum(sTemp.Substring(1))
        'sChecksum = Checksum.ToString("X02")
        'arrSTEMP(0) = [String].Concat(sTemp, sChecksum, vbCrLf)
        'sTemp = [String].Concat(Chr(2), "2", PatientRecord, vbCr, Chr(3))
        'Checksum = StringChecksum(sTemp.Substring(1))
        'sChecksum = Checksum.ToString("X02")
        'arrSTEMP(1) = [String].Concat(sTemp, sChecksum, vbCrLf)
        'Dim FrameIndex As Integer = 1
        'If OrderRecord.Length > 220 Then
        '    While OrderRecord.Length > 220
        '        arrOrder.Add(OrderRecord.Substring(0, 220))
        '        OrderRecord = OrderRecord.Remove(0, 220)
        '    End While
        '    arrOrder.Add(OrderRecord)
        '    ReDim Preserve arrSTEMP(arrOrder.Count + 2)
        '    For FrameIndex = 0 To arrOrder.Count - 2
        '        sTemp = [String].Concat(Chr(2), (FrameIndex + 3).ToString.Trim, arrOrder(FrameIndex), vbCr, Chr(ETB))
        '        Checksum = StringChecksum(sTemp.Substring(1))
        '        sChecksum = Checksum.ToString("X02")
        '        arrSTEMP(FrameIndex + 2) = [String].Concat(sTemp, sChecksum, vbCrLf)
        '    Next FrameIndex
        '    sTemp = [String].Concat(Chr(2), (FrameIndex + 3).ToString.Trim, arrOrder(FrameIndex), vbCr, Chr(ETX))
        '    Checksum = StringChecksum(sTemp.Substring(1))
        '    sChecksum = Checksum.ToString("X02")
        '    arrSTEMP(FrameIndex + 2) = [String].Concat(sTemp, sChecksum, vbCrLf)
        '    FrameIndex += 1
        'Else
        '    sTemp = [String].Concat(Chr(2), "3", OrderRecord, vbCr, Chr(3))
        '    Checksum = StringChecksum(sTemp.Substring(1))
        '    sChecksum = Checksum.ToString("X02")
        '    arrSTEMP(2) = [String].Concat(sTemp, sChecksum, vbCrLf)
        'End If
        'sTemp = [String].Concat(Chr(2), (FrameIndex + 3).ToString.Trim, TerminationRecord, vbCr, Chr(3))
        'Checksum = StringChecksum(sTemp.Substring(1))
        'sChecksum = Checksum.ToString("X02")
        'arrSTEMP(FrameIndex + 2) = [String].Concat(sTemp, sChecksum, vbCrLf)
        'Return arrSTEMP
        Dim sTemp, sChecksum As String
        Dim Checksum As Byte = 0
        Dim arrSTEMP(3) As String
        sTemp = [String].Concat(Chr(2), "1", HeaderRecord, vbCr, Chr(3))
        Checksum = StringChecksum(sTemp.Substring(1))
        sChecksum = Checksum.ToString("X02")
        arrSTEMP(0) = [String].Concat(sTemp, sChecksum, vbCrLf)
        sTemp = [String].Concat(Chr(2), "2", PatientRecord, vbCr, Chr(3))
        Checksum = StringChecksum(sTemp.Substring(1))
        sChecksum = Checksum.ToString("X02")
        arrSTEMP(1) = [String].Concat(sTemp, sChecksum, vbCrLf)
        sTemp = [String].Concat(Chr(2), "3", OrderRecord, vbCr, Chr(3))
        Checksum = StringChecksum(sTemp.Substring(1))
        sChecksum = Checksum.ToString("X02")
        arrSTEMP(2) = [String].Concat(sTemp, sChecksum, vbCrLf)
        sTemp = [String].Concat(Chr(2), "4", TerminationRecord, vbCr, Chr(3))
        Checksum = StringChecksum(sTemp.Substring(1))
        sChecksum = Checksum.ToString("X02")
        arrSTEMP(3) = [String].Concat(sTemp, sChecksum, vbCrLf)
        Return arrSTEMP
    End Function
    Public Overloads Function CreateSinglePatientFrame(ByVal HeaderRecord As String, ByVal PatientRecord As String, ByRef OrderRecord As ArrayList, ByVal TerminationRecord As String) As String()
        Dim sTemp, sChecksum As String
        Dim Checksum As Byte = 0
        Dim arrSTEMP(4) As String
        sTemp = [String].Concat(Chr(2), "1", HeaderRecord, vbCr, Chr(3))
        Checksum = StringChecksum(sTemp.Substring(1))
        sChecksum = Checksum.ToString("X02")
        arrSTEMP(0) = [String].Concat(sTemp, sChecksum, vbCrLf)
        sTemp = [String].Concat(Chr(2), "2", PatientRecord, vbCr, Chr(3))
        Checksum = StringChecksum(sTemp.Substring(1))
        sChecksum = Checksum.ToString("X02")
        arrSTEMP(1) = [String].Concat(sTemp, sChecksum, vbCrLf)

        sTemp = [String].Concat(Chr(2), "3", OrderRecord(0), vbCr, Chr(3))
        Checksum = StringChecksum(sTemp.Substring(1))
        sChecksum = Checksum.ToString("X02")
        arrSTEMP(2) = [String].Concat(sTemp, sChecksum, vbCrLf)

        sTemp = [String].Concat(Chr(2), "4", OrderRecord(1), vbCr, Chr(3))
        Checksum = StringChecksum(sTemp.Substring(1))
        sChecksum = Checksum.ToString("X02")
        arrSTEMP(3) = [String].Concat(sTemp, sChecksum, vbCrLf)

        sTemp = [String].Concat(Chr(2), "5", TerminationRecord, vbCr, Chr(3))
        Checksum = StringChecksum(sTemp.Substring(1))
        sChecksum = Checksum.ToString("X02")
        arrSTEMP(4) = [String].Concat(sTemp, sChecksum, vbCrLf)

        Return arrSTEMP
    End Function

    Public Function CreateNegQueryReportFrame(ByVal HeaderRecord As String, ByVal QueryRecord As String, ByVal TerminationRecord As String) As String()
        Dim sTemp, sChecksum As String
        Dim Checksum As Byte = 0
        Dim arrSTEMP(2) As String
        sTemp = [String].Concat(Chr(2), "1", HeaderRecord, vbCr, Chr(3))
        Checksum = StringChecksum(sTemp.Substring(1))
        sChecksum = Checksum.ToString("X02")
        arrSTEMP(0) = [String].Concat(sTemp, sChecksum, vbCrLf)
        sTemp = [String].Concat(Chr(2), "2", QueryRecord, vbCr, Chr(3))
        Checksum = StringChecksum(sTemp.Substring(1))
        sChecksum = Checksum.ToString("X02")
        arrSTEMP(1) = [String].Concat(sTemp, sChecksum, vbCrLf)
        sTemp = [String].Concat(Chr(2), "3", TerminationRecord, vbCr, Chr(3))
        Checksum = StringChecksum(sTemp.Substring(1))
        sChecksum = Checksum.ToString("X02")
        arrSTEMP(2) = [String].Concat(sTemp, sChecksum, vbCrLf)
        Return arrSTEMP
    End Function

    Public Sub ResultRecordParse(ByVal sInput() As String, ByRef UniversalTestID() As String, ByRef DataValue As String, ByRef Units As String, ByRef RefRange As String, ByRef ResultAbnormalFlags As String, ByRef ResultStatus As String, ByRef DateTestCompleted As String, ByRef WhichModule As String)
        UniversalTestID = sInput(2).Split(ComponentDelimiter)
        DataValue = sInput(3)
        Units = sInput(4)
        RefRange = sInput(5)
        ResultAbnormalFlags = sInput(6)
        ResultStatus = sInput(8)
        DateTestCompleted = sInput(12)
        WhichModule = sInput(13)
    End Sub

    Public Sub OrderRecordParse(ByVal sInput() As String, ByRef SpecimenID() As String, ByRef UniversalTestID() As String)
        Try
            Dim Temp2() As String
            SpecimenID = sInput(3).Split(ComponentDelimiter)
            Dim Temp() As String = sInput(4).Split(RepeatDelimiter)
            ReDim UniversalTestID(Temp.GetLength(0) - 1)
            For i As Byte = 0 To Temp.GetLength(0) - 1
                Temp2 = Temp(i).Split(ComponentDelimiter)
                UniversalTestID(i) = Temp2(3)
            Next i
        Catch ex As Exception

        End Try
    End Sub

    Public Sub PatientRecordParse(ByVal sInput() As String, ByRef PID As String, ByRef PatientName() As String, ByRef DOB As String, ByRef Sex As Char, ByRef PhysicianID As String, ByRef Location As String)
        Dim iCount As Byte = sInput.GetLength(0)
        If iCount > 2 Then
            PID = sInput(2)
        End If
        If iCount > 5 Then
            PatientName = sInput(5).Split(ComponentDelimiter)
        End If
        If iCount > 7 Then
            DOB = sInput(7)
        End If
        If iCount > 8 Then
            Sex = sInput(8)
        End If
        If iCount > 13 Then
            PhysicianID = sInput(13)
        End If
        If iCount > 25 Then
            Location = sInput(25)
        End If

    End Sub

    Public Sub ResultDataParse(ByVal sInput As ArrayList, ByRef PatientStructure As Patient)
        'Dim sTemp() As String = sInput.Split(RecordDelimiter)
        Dim sTemp2() As String = {}
        Dim s() As String
        Dim NoOfTest As Byte = 0
        Dim NoOfResult As Byte = 0
        Dim Aspect As Byte = 0
        Dim UniversalTestID() As String = {}
        Dim DataValue As String = [String].Empty
        Dim Units As String = [String].Empty
        Dim AllergyClassRange As String = [String].Empty
        Dim AbnormalFlags As String = [String].Empty
        Dim ResultStatus As String = [String].Empty
        Dim DateTestCompleted As String = [String].Empty
        Dim WhichModule As String = [String].Empty
        'Dim Finded As Boolean = False
        With PatientStructure
            For i As Byte = 0 To sInput.Count - 1
                s = sInput(i).Split(FieldDelimiter)
                Select Case s(0)
                    Case PatientRecordType
                        PatientRecordParse(s, .PID, .PatientName, .DateOfBirth, .Sex, .PhysicianID, .Location)
                    Case OrderRecordType
                        OrderRecordParse(s, .SpecimenID, sTemp2)
                        For Each str As String In sTemp2
                            ReDim Preserve .ResultArray(NoOfTest)
                            .ResultArray(NoOfTest).TestCode = str
                            NoOfTest += 1
                        Next
                    Case ResultRecordType
                        ResultRecordParse(s, UniversalTestID, DataValue, Units, AllergyClassRange, AbnormalFlags, ResultStatus, DateTestCompleted, WhichModule)
                        'For k As Byte = 0 To NoOfTest - 1
                        '    If .ResultArray(k).TestCode = UniversalTestID(3) Then
                        'With .ResultArray(k)
                        With .ResultArray(NoOfTest - 1)
                            Select Case UniversalTestID(10)
                                Case "F"
                                    Aspect = 0
                                Case "P"
                                    Aspect = 1
                                Case "I"
                                    Aspect = 2
                                Case Else
                                    Aspect = 0
                            End Select
                            ReDim Preserve .DataValue(7)
                            .DataValue(Aspect) = DataValue
                            .Unit = Units
                            .DateTestCompleted = DateTestCompleted
                            .WhichModule = WhichModule
                            .BarcodeTest = PatientStructure.SpecimenID(0)
                        End With
                End Select
            Next
        End With

    End Sub



End Class
